package com.filemanager;

import com.filemanager.command.Command;
import com.filemanager.command.PossibleCommands;
import com.filemanager.exceptions.ChangeDirectoryException;
import com.filemanager.exceptions.DirectoryCreationException;
import com.filemanager.exceptions.FileCreationException;
import com.filemanager.exceptions.UnsupportedCommandException;

import java.io.File;
import java.util.Collection;
import java.util.Scanner;

/**
 * User: vsinjavin
 * Date: 11/15/13
 */
public class ConsoleMonitor {

    private final FileManager fileManager;

    private final Scanner scanner;

    public ConsoleMonitor(Scanner scanner, FileManager fileManager) {
        this.fileManager = fileManager;
        this.scanner = scanner;
    }


    public void startMonitor() {

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();

            if(line.equals("exit"))
                break;

            try {
                executeCommand(getCommand(line));
            } catch (UnsupportedCommandException e) {
                //TODO : add some specific logic here
                System.out.println(e.getMessage());
            } catch (DirectoryCreationException e) {
                System.out.println(e.getMessage());
            } catch (FileCreationException e) {
                System.out.println(e.getMessage());
            } catch (ChangeDirectoryException e) {
                System.out.println(e.getMessage());
            }
        }

        scanner.close();
    }

    private Command getCommand(String line) throws UnsupportedCommandException {

        String[] split = line.split(" ");

        PossibleCommands possibleCommand = PossibleCommands.convert(split[0]);

        if(split.length > 1)
            return new Command(possibleCommand, split[1]);
        else
            return new Command(possibleCommand);
    }

    private void executeCommand(Command command) throws DirectoryCreationException, FileCreationException, ChangeDirectoryException {
       switch (command.getCommand()){
           case CD:
               fileManager.changeDirectory(command.getParams());
               break;
           case LS:
               Collection<File> fileAndFolder;
               if(command.hasParams())
                   fileAndFolder = fileManager.getDirectoryStructure(command.getParams());
               else
                    fileAndFolder = fileManager.getDirectoryStructure();

               for(File file : fileAndFolder){
                   if(file.isDirectory())
                       System.out.println(String.format("Dir:%s", file.getName()));
                   else
                       System.out.println(String.format("File:%s", file.getName()));
               }

               break;
           case MKDIR:
               fileManager.createDirectory(command.getParams());
               break;
           case TOUCH:
               fileManager.createFile(command.getParams());
               break;
           case PWD:
               System.out.println(fileManager.currentPath());
               break;
       }
    }

}
